//
//  LiustatuseViewModel.swift
//  LiuWeiBo
//
//  Created by 刘晓晨 on 2017/3/30.
//  Copyright © 2017年 刘晓晨. All rights reserved.
//

import UIKit

class LiustatuseViewModel: NSObject {

    var homeStatuse: LiuStatus? {
        didSet {
            //  获取认证类型的数据
            let verifiedType = homeStatuse?.user?.verified_type
            //  处理认证类型图片的图片
            handleVerifiedTypeImage(verifiedType: verifiedType!)
            //  获取会员等级
            let mbrank = homeStatuse?.user?.mbrank ?? 0
            //  处理会员等级图片 1-6
            handleMbrankImage(mbrank: mbrank)
            
            setRepostsCountStr()
            setCommentsCountStr()
            setAttitudesCountStr()
            setContentStr()
            setSourceContent()
        }
    }

    var verifiedTypeImage:UIImage?
    
    var mbrankImage:UIImage?

    var repostsCountStr:String?
    
    var commentsCountStr:String?
    
    var attitudesCountStr:String?
    
    var contentStr:NSAttributedString?
    
    var sourceContent:String?
    
    //每次获取时间都要跟当前时间对比,所以使用计算属性get方法
    var timeContent:String? {
        //判断时间是否为空
        guard let create = homeStatuse?.created_at else {
            return nil
        }
        
        //创建时间格式化对象
        let dateformat = DateFormatter()
        
        //判断是否是今年的微博
        if isThisEary(date:create) {
            //是同一年
            
            //创建calendar对象
            let calendar = Calendar.current
            
            //是否为同一天
            if calendar.isDateInToday(create) {
                //是同一天
                
                //获取和当前时间的时间差(s)
                let timeDifference = abs(create.timeIntervalSinceNow)
                
                //判断具体时间
                if timeDifference < 60 {
                    //一分钟之内 "刚刚"
                    return "刚刚"
                } else if timeDifference < 3600 {
                    //一小时之内 "分钟数 + 之前"
                    return "\(Int(timeDifference/60))分钟之前"
                } else {
                    //一小时之外
                    return "\(Int(timeDifference/3600))小时之前"
                }
            } else if calendar.isDateInYesterday(create) {
                //是前一天
                //显示格式为"昨天 HH:mm"
                dateformat.dateFormat = "昨天 HH:mm"
                return dateformat.string(from: create)
            } else {
                //是两天之前
                //显示的格式为"MM-dd HH:mm"
                dateformat.dateFormat = "MM-dd HH:mm"
                return dateformat.string(from: create)
            }
            
            
            
        } else {
            //不是同一年
            //显示效果为"yyyy-MM-dd HH:mm"
            dateformat.dateFormat = "yyyy-MM-dd HH:mm"
            return dateformat.string(from: create)
            
        }
        
    }
    
    //tipLabel显示内容
//    var tipContent:String?
    
    
    
    
    fileprivate func isThisEary(date:Date)->Bool {
        
        //创建时间格式化对象
        let dateformat = DateFormatter()
        
        //设计时间的显示格式
        dateformat.dateFormat = "yyyy"
        
        //获取当前时间的年份
        let createEary = dateformat.string(from: date)
        
        //获取当前时间的年份
        let nowEary = dateformat.string(from: Date())
        
        return createEary == nowEary
    }
    
    fileprivate func setSourceContent() {
        if let startRange = homeStatuse?.source?.range(of: "\">"), let endRange = homeStatuse?.source?.range(of: "</") {
            //开始的下标
            let startIndex = startRange.upperBound
            
            //结束的下标
            let endIndex = endRange.lowerBound
            
            //拼接字符串
            
            sourceContent = "来自: " + (homeStatuse?.source?.substring(with: startIndex ..< endIndex))! 
            
            
        }
    }
    
    
    fileprivate func setContentStr() {
        if let contentText = homeStatuse?.retweeted_status?.text, let name = homeStatuse?.retweeted_status?.user?.screen_name {
            let str:NSString = "@\(name) \(contentText)" as NSString
            let range = str.range(of: "@\(name)")
            
            let attrabuteStr = NSMutableAttributedString(string: str as String)
            
            attrabuteStr.addAttributes([NSForegroundColorAttributeName:UIColor.blue], range: range)
            
            contentStr = attrabuteStr
        }
    }
    
    
    fileprivate func setRepostsCountStr() {
        repostsCountStr = (homeStatuse?.reposts_count)! > 0 ? "\((homeStatuse?.reposts_count)!)":"转发"
    }
    
    fileprivate func setCommentsCountStr() {
        commentsCountStr = (homeStatuse?.comments_count)! > 0 ? "\((homeStatuse?.comments_count)!)":"评论"
    }
    
    fileprivate func setAttitudesCountStr() {
        attitudesCountStr = (homeStatuse?.attitudes_count)! > 0 ? "\((homeStatuse?.attitudes_count)!)":"赞"
    }
    
    
    fileprivate func handleVerifiedTypeImage(verifiedType:Int) {
        //根据数据判断使用什么图片,//  认证类型 -1：没有认证，1，认证用户，2,3,5: 企业认证，220: 达人
        switch verifiedType {
        case 1:
            verifiedTypeImage = UIImage(named: "avatar_vip")
        case 2,3,5:
            verifiedTypeImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifiedTypeImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedTypeImage = nil
        }
    }
    
    fileprivate func handleMbrankImage(mbrank:Int) {
        if mbrank >= 1 && mbrank <= 6 {
            mbrankImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
    }
    
}
